package com.example.restaurantclient.Fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.anshi.shopchangepro.MainActivity;
import com.example.restaurantclient.Adapter.ShopcartAdapter;
import com.example.restaurantclient.Entities.ShopCart;
import com.example.restaurantclient.Entities.Variable;
import com.example.restaurantclient.R;

import java.util.ArrayList;
import java.util.List;

//
public class CartFragment extends Fragment {
    private View view;
    public static List<ShopCart> shopCarts=new ArrayList<>();
    private ShopcartAdapter adapter;
    private ListView listView;
    private Button btnCommit;
    private TextView tvAllmoney;
    private List<Integer> cakeIds=new ArrayList<>();//存储所要勾选的蛋糕id
    private List<Integer> cakenums=new ArrayList<>();//存储所要勾选蛋糕的购买数量
    private double total=0;//合计
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            for(int i=0;i<shopCarts.size();i++){
                if(msg.what==i){
                    double money=Double.parseDouble(msg.obj.toString());
                    if(money>0){
                        cakeIds.add(msg.what);
                        Log.e("arg1的值",msg.arg1+"");
                        Log.e("msg.what的值",msg.what+"");
                        cakenums.add(msg.arg1);
                    }else{
                        cakeIds.remove(cakeIds.indexOf(msg.what));
                        cakenums.remove(cakenums.indexOf(msg.arg1));
                    }
                    total=total+money;
                }
            }
            tvAllmoney.setText(String.format("%.2f",total));
        }
    };
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view==null) {
            view = inflater.inflate(R.layout.fragment_cart, container, false);

        }
        listView=view.findViewById(R.id.lv_cartlist);
        btnCommit=view.findViewById(R.id.btn_commitorder);
        tvAllmoney=view.findViewById(R.id.tv_allmoney);
        if(shopCarts.size()==0){
            initData();
        }

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
                //定义AlertDialog.Builder对象，当长按列表项的时候弹出确认删除对话框
                AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
                builder.setMessage("确定删除?");
                builder.setTitle("提示");

                //添加AlertDialog.Builder对象的setPositiveButton()方法
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(shopCarts.remove(i)!=null){
                            System.out.println("success");
                        }else {
                            System.out.println("failed");
                        }
                        adapter.notifyDataSetChanged();
                        Toast.makeText(getContext(), "删除列表项", Toast.LENGTH_SHORT).show();
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.create().show();
                return false;

            }
        });
        adapter=new ShopcartAdapter(getContext(),shopCarts,R.layout.item_shopcart,handler);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        //提交订单的按钮事件
        btnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(getContext()).setTitle("支付确认")//设置对话框标题

                        .setMessage("是否支付")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {//添加确定按钮

                            @Override
                            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                if(Variable.sumPrice<total){
                                    Toast.makeText(getContext(),"余额不足，请充值",Toast.LENGTH_SHORT).show();
                                }else{
                                    Toast.makeText(getContext(),"结算成功",Toast.LENGTH_SHORT).show();
                                    Variable.sumPrice-=total;
                                    Variable.jiFen+=total;

                                }
                            }
                        }).setNegativeButton("否", new DialogInterface.OnClickListener() {//添加返回按钮

                    @Override
                    public void onClick(DialogInterface dialog, int which) {//响应事件

                    }

                }).show();//在按键响应事件中显示此对话框
            }
        });

        return view;
    }
    private void initData() {
        ShopCart s1=new ShopCart(1,1,1);
        ShopCart s2=new ShopCart(1,3,1);
        ShopCart s3=new ShopCart(1,5,1);
        shopCarts.add(s1);
        shopCarts.add(s2);
        shopCarts.add(s3);
    }
    @Override
    public void onStart() {
        total=0;
        tvAllmoney.setText(String.format("%.2f",total));
        super.onStart();
    }
}
